[LeetCode] 708. Insert into a Cyclic Sorted List 在循环有序的链表中插入结点

 

Given a node from a Circular Linked List which is sorted in ascending order, write a function to insert a value insertVal into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list, and may not be necessarily the smallest value in the circular list.

If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the circular list should remain sorted.

If the list is empty (i.e., given node is null), you should create a new single circular list and return the reference to that single node. Otherwise, you should return the original given node.

 

Example 1:


 

Input: head = [3,4,1], insertVal = 2
Output: [3,4,1,2]
Explanation: In the figure above, there is a sorted circular list of three elements. You are given a reference to the node with value 3, and we need to insert 2 into the list. The new node should be inserted between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3.

Example 2:

Input: head = [], insertVal = 1
Output: [1]
Explanation: The list is empty (given head is null). We create a new single circular list and return the reference to that single node.

Example 3:

Input: head = [1], insertVal = 0
Output: [1,0]

 

Constraints:

  • 0 <= Number of Nodes <= 5 * 10^4
  • -10^6 <= Node.val <= 10^6
  • -10^6 <= insertVal <= 10^6

 

这道题让在循环有序的链表中插入结点,要求插入结点后,链表仍保持有序且循环。题目中强调了 corner case 的情况,就是当链表为空时,插入结点即要生成一个新的循环有序链表,那么可以先处理这个特殊情况,比较简单,就是新建一个结点,然后将 next 指针指向自己即可。好,下面来看给定的链表不为空的情况,最常见的情况就是要插入的结点值在两个有序结点值 [a, b] 之间,那么只要满足 a <= insertVal <= b 即可。由于是循环有序的链表,结点值不会一直上升,到某一个结点的时候,是最大值,但是下一个结点就是最小值了,就是题目中的例子,结点4到结点1的时候,就是下降了。那么在这个拐点的时候,插入值 insertVal 就会有两种特殊的情况,其大于等于最大值,或者小于等于最小值,比如插入值是5,或者0的时候,这两种情况都插入在结点4之后,可以放一起处理。而若其小于最大值,或者大于最小值,就是上面那种一般的情况,不会在这里处理,所以只要检测如果属于上面的两种情况之一,就 break 掉循环,进行插入结点处理即可,参见代码如下:

 

class Solution {
public:
    Node* insert(Node* head, int insertVal) {
        if (!head) {
            head = new Node(insertVal, NULL);
            head->next = head;
            return head;
        }
        Node *pre = head, *cur = pre->next;
        while (cur != head) {
            if (pre->val <= insertVal && cur->val >= insertVal) break;
            if (pre->val > cur->val && (pre->val <= insertVal || cur->val >= insertVal)) break;
            pre = cur;
            cur = cur->next;
        }
        pre->next = new Node(insertVal, cur);
        return head;
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/708

 

类似题目:

Insertion Sort List

 

参考资料:

https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/

https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/discuss/189351/java-one-pass-solution-and-easy-understand

https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/discuss/136918/(Accepted)-C%2B%2B-Solution-in-O(n)-with-Explanation

 

LeetCode All in One 题目讲解汇总(持续更新中...)

posted @ 2018-11-19 01:15  Grandyang  阅读(6036)  评论(0编辑  收藏  举报
Fork me on GitHub